package cn.com.oceansoft.business.danger.controller;

import cn.com.oceansoft.business.base.controller.BaseController;
import cn.com.oceansoft.business.base.entity.Query;
import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.danger.entity.Attach;
import cn.com.oceansoft.business.danger.entity.CollecterInfo;
import cn.com.oceansoft.business.danger.entity.Wzzj;
import cn.com.oceansoft.business.danger.service.ICollecterInfoService;
import cn.com.oceansoft.business.danger.service.IWzzjService;
import cn.com.oceansoft.business.mobile.danger.util.PicZzThread;
import cn.com.oceansoft.nos.common.util.HttpClientUtil;
import cn.com.oceansoft.nos.common.util.base64.Base64;
import freemarker.template.utility.StringUtil;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import sun.misc.BASE64Encoder;
import sun.misc.Request;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * Created by OS on 2018/5/18.
 */

@Controller
@RequestMapping("danger")
public class WzzjController extends BaseController{

    @Resource
    private IWzzjService wzzjService;
    @Resource
    private ICollecterInfoService collecterInfoService;

    @RequestMapping("wzzj/wzzjlist")
    public String wzzjlist(Model model, Query query,CollecterInfo collecterInfo,HttpServletRequest request) {
        int mark = 0;

        if ("".equals(collecterInfo.getFjid()) || null == collecterInfo.getFjid()) {
            collecterInfo.setFjid(getMemberSession().getFjid());
            // collecterInfo=getMemberSession();
            mark = 1;
            if (getMemberSession().getRoletype().equals("1")) {
                collecterInfo.setPcsid(getMemberSession().getPcsid());
                collecterInfo.setSqid("");
            } else if (getMemberSession().getRoletype().equals("2")) {
                collecterInfo.setPcsid(getMemberSession().getPcsid());
                collecterInfo.setSqid(getMemberSession().getSqid());
            } else {
                collecterInfo.setPcsid("");
                collecterInfo.setSqid("");
            }

        }
        query.setData(collecterInfo);
        model.addAttribute("query", query);
        model.addAttribute("pager",wzzjService.getWzzjList(query));
        if ("".equals(collecterInfo.getFjid()) || null == collecterInfo.getFjid()) {
            model.addAttribute("dicone", collecterInfoService.getdict(collecterInfo));
        } else {
            CollecterInfo one = new CollecterInfo();
            one.setFjid(collecterInfo.getFjid());
            if ("".equals(getMemberSession().getFjid()) || null == getMemberSession().getFjid()) {
                CollecterInfo collecterInfo1 = new CollecterInfo();
                collecterInfo1.setFjid("");
                model.addAttribute("dicone", collecterInfoService.getdict(collecterInfo1));
            } else {
                model.addAttribute("dicone", collecterInfoService.getonedict(one));
            }
            if (getMemberSession().getRoletype().equals("1") || getMemberSession().getRoletype().equals("2")) {
                CollecterInfo collecterInfo1 = new CollecterInfo();
                collecterInfo1.setFjid(getMemberSession().getPcsid());
                model.addAttribute("dictwo", collecterInfoService.getonedict(collecterInfo1));
            } else {
                model.addAttribute("dictwo", collecterInfoService.getdict(one));
            }
            if (getMemberSession().getRoletype().equals("1")) {
                collecterInfo.setPcsid(getMemberSession().getPcsid());
                if (!"".equals(collecterInfo.getPcsid()) && null != collecterInfo.getPcsid()) {
                    one.setPcsid(collecterInfo.getPcsid());
                    model.addAttribute("dicthree", collecterInfoService.getdict(collecterInfo));
                }
            } else if (getMemberSession().getRoletype().equals("2")) {
                CollecterInfo collecterInfo1 = new CollecterInfo();
                collecterInfo1.setFjid(getMemberSession().getSqid());
                model.addAttribute("dicthree", collecterInfoService.getonedict(collecterInfo1));
            } else {
                if (!"".equals(collecterInfo.getPcsid()) && null != collecterInfo.getPcsid()) {
                    one.setPcsid(collecterInfo.getPcsid());
                    model.addAttribute("dicthree", collecterInfoService.getdict(collecterInfo));
                }
            }
        }

        if("1".equals(collecterInfo.getWzzjlb())){
            return "danger/wzzj/info_ytlist";
        }else{
            return "danger/wzzj/info_zzlist";
        }
    }

    @RequestMapping("wzzj/editWzzj")
    public String editWzzj(Model model, HttpServletRequest request, String id, String type) {
        Wzzj wzzj = new Wzzj();
        Attach attach = new Attach();
        attach.setOrderdbid(id);
        attach.setType("12");
        List<Attach> list = new ArrayList<>();
        if(id!=null){
            wzzj = wzzjService.findWzzjById(id);
            list = wzzjService.findAttachList(attach);
        }
        model.addAttribute("wzzj",wzzj);
        model.addAttribute("photoList",list);
        CollecterInfo collecterInfo = new CollecterInfo();
        model.addAttribute("dicone",collecterInfoService.getdict(collecterInfo));
        collecterInfo.setFjid(wzzj.getFjid());
        model.addAttribute("dictwo",collecterInfoService.getdict(collecterInfo));
        collecterInfo.setPcsid(wzzj.getPcsid());
        model.addAttribute("dicthree",collecterInfoService.getdict(collecterInfo));
        if(type!=null){
            if(type.equals("1")){
                return "danger/wzzj/editYtjl";
            }else{
                return "danger/wzzj/editZzjl";
            }
        }else{
            return null;
        }
    }

    @ResponseBody
    @RequestMapping(value="wzzj/editWzzjform", method= RequestMethod.POST)
    public Result editWzzjform(Model model, HttpServletRequest request, Wzzj wzzj) {
        wzzj.setUserid(getMemberSession().getId());
        Result result =  new Result();
        result.setMsg("操作成功!");
        try {
            BASE64Encoder base64 = new BASE64Encoder();
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files = multipartRequest.getFiles("file");
            if ("".equals(wzzj.getId()) || null == wzzj.getId()) {
                String id = UUID.randomUUID().toString();
                wzzj.setId(id);
                wzzjService.addWzzj(wzzj);
            } else {
                wzzjService.editWzzj(wzzj);
            }
            for (int i=0;i<files.size();i++) {
                MultipartFile file = files.get(i);
                if(file.getSize()!=0){
                    PicZzThread thread=new PicZzThread(wzzj,file);
                    thread.start();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setSucc(false);
            result.setMsg("操作失败!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("wzzj/deleteWzzj")
    public Result deleteWzzj(Model model, HttpServletRequest request, String id ,String type) {
        Result result =  new Result();
        result.setMsg("操作成功!");
        try {
            wzzjService.deleteWzzj(id);
        }catch (Exception e){
            e.printStackTrace();
            result.setSucc(false);
            result.setMsg("操作失败!");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("wzzj/deletePic")
    public Result deletePic(Model model, HttpServletRequest reques,String id) {
        Result result =  new Result();
        result.setMsg("操作成功!");
        try {
            wzzjService.deletePic(id);
        }catch (Exception e){
            e.printStackTrace();
            result.setSucc(false);
            result.setMsg("操作失败!");
        }
        return result;
    }
}
